//
//  UIButton+ZJIconfont.m
//  IconfontDemo
//
//  Created by ZJ on 2018/7/20.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "UIButton+ZJIconfont.h"
#import "ZJEncodingString.h"

static const CGFloat kDefaultWidth = 44;

@implementation UIButton (ZJIconfont)

- (instancetype)initWithFrame:(CGRect)frame unicode:(NSString *)code {
    return [self initWithFrame:frame unicode:code titleColor:nil iconfontSize:0];
}

- (instancetype)initWithFrame:(CGRect)frame unicode:(NSString *)code iconfontSize:(CGFloat)size {
    return [self initWithFrame:frame unicode:code titleColor:nil iconfontSize:size];
}

- (instancetype)initWithFrame:(CGRect)frame unicode:(NSString *)code titleColor:(UIColor *)color {
    return [self initWithFrame:frame unicode:code titleColor:color iconfontSize:0];
}

- (instancetype)initWithFrame:(CGRect)frame unicode:(NSString *)code titleColor:(UIColor *)color iconfontSize:(CGFloat)size
{
    return [self initWithFrame:frame unicode:code titleColor:color titleHighlightedColor:nil iconfontSize:size];
}

- (instancetype)initWithFrame:(CGRect)frame unicode:(NSString *)code titleColor:(UIColor *)color titleHighlightedColor:(UIColor *)highlightedColor iconfontSize:(CGFloat)size {
    frame = [self sizeIsZero:frame.size] ? CGRectMake(frame.origin.x, frame.origin.y, kDefaultWidth, kDefaultWidth) : frame;
    self = [super initWithFrame:frame];
    if (self) {
        [self setTitle:code forState:UIControlStateNormal];
        [self setTitle:code forState:UIControlStateHighlighted];
        
        size = size ? size : kDefaultWidth;
        CGFloat iconfontMaxSize = MIN(frame.size.width, frame.size.height);
        iconfontMaxSize = MIN(iconfontMaxSize, size);
        
        self.titleLabel.font = [UIFont fontWithName:@"iconfont" size:iconfontMaxSize];
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        color = color ? color : [UIColor darkGrayColor];
        highlightedColor = highlightedColor ? highlightedColor : [UIColor lightGrayColor];
        
        [self setTitleColor:color forState:UIControlStateNormal];
        [self setTitleColor:highlightedColor forState:UIControlStateHighlighted];
    }
    return self;
}

- (BOOL)sizeIsZero:(CGSize)size {
    return (size.width == CGSizeZero.width) && (size.height == CGSizeZero.height);
}

/**********************************************************************************/

- (NSString *)titleWithCode:(NSString *)code {
    NSString *title = code;
    
    if (![self isUnicode:title]) {
//        title = [ZJEncodingString stringForIcon:title.UTF8String];
//        title = [@"\\u" stringByAppendingString:code];
        char buffer[64] = {0};
        sprintf(buffer, "\\u%s", title.UTF8String);
        title = [NSString stringWithUTF8String:buffer];
    }
    
    return title;
}


- (BOOL)isUnicode:(NSString*)str
{
    int strlength = 0;
    char* p = (char *)[str cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i = 0 ; i < [str lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) {
        if (*p) {
            p++;
            strlength++;
        } else {
            p++;
        }
    }
    
    return ((strlength / 2) == 1) ? YES : NO;
}

@end
